package com.chufuying.tools.services.impl;

import com.chufuying.tools.beans.ColumnInfoForTable;
import com.chufuying.tools.beans.GlobalClass;
import com.chufuying.tools.services.CreateClass;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @autor: Faizel
 * @date: 2017/3/4
 * @version: 1.0.0
 */
public class CreateClassImpl implements CreateClass {

    @Override
    public void createClass(List<ColumnInfoForTable> columnList, String table) {
            changeType(columnList);
            outPutClass(columnList, System.getProperty("user.dir") + GlobalClass.getClassfilepath(), table);
    }

    private void changeType(List<ColumnInfoForTable> columnList) {

        for (ColumnInfoForTable cift:columnList) {
            String firstChar = cift.getColumnName().charAt(0) + "";
            /** 将列名首字符大写 **/
            cift.setNameForGS(cift.getColumnName().replaceFirst(firstChar, firstChar.toUpperCase()));
            /** 类型转化 **/
            switch (cift.getColumnTypeCode()) {
                case -1:
                    cift.setColumnType("String");
                    break;
                case 4:
                    cift.setColumnType("Integer");
                    break;
                case -6:
                    cift.setColumnType("Integer");
                    break;
                case 12:
                    cift.setColumnType("String");
                    break;
                case -7:
                    cift.setColumnType("Boolean");
                    break;
                case 5:
                    cift.setColumnType("Integer");
                    break;
                case -5:
                    cift.setColumnType("Long");
                    break;
                case 8:
                    cift.setColumnType("Double");
                    break;
                case 7:
                    cift.setColumnType("Float");
                    break;
                case 1:
                    cift.setColumnType("String");
                    break;
                case 91:
                    cift.setColumnType("Date");
                    break;
                case 92:
                    cift.setColumnType("Time");
                    break;
                case 93:
                    cift.setColumnType("Timestamp");
                    break;
                case -4:
                    cift.setColumnType("byte[]");
                    break;
                case 3:
                    cift.setColumnType("BigDecimal");
                    break;
                default:
                    cift.setColumnType("String");
                    break;
            }
        }
    }


    private void outPutClass(List<ColumnInfoForTable> list, String outPath, String table) {
        FileOutputStream out = null;
        try {
            String javaFile = outPath + table + ".java";
            out=new FileOutputStream(javaFile);
            String tab = "\t";
            String sep = System.getProperty("line.separator");
            String className = table;
            StringBuffer content = new StringBuffer("package " +
                                                    GlobalClass.getClassfilepath().substring(1, GlobalClass.getClassfilepath().length() - 1).replaceAll("/", ".")
                                                    + " * * *" + sep);
            content.append("public class " + className + " {" + sep);
            StringBuffer sgString = new StringBuffer(tab + "/** set/get **/" + sep);
            for(ColumnInfoForTable tem:list){
                content.append(tab + "private " + tem.getColumnType() + " " + tem.getColumnName() + ";" + sep);
                /** 生成set方法 **/
                sgString.append(tab + "public void set" + tem.getNameForGS() + "(" + tem.getColumnType() + " "
                        + tem.getColumnName() + ") {" + sep + tab + tab+ "this." + tem.getColumnName() + " = "
                        + tem.getColumnName() + ";" + sep + tab + "}" + sep);
                /** 生成get方法 **/
                sgString.append(tab + "public " + tem.getColumnType() + " get" +
                        tem.getNameForGS() + "() {" + sep + tab + tab + "return " + tem.getColumnName() + ";"
                        + sep + tab + "}" + sep);
            }
            content.append(sgString);
            content.append("}");
            out.write(content.toString().getBytes("utf-8"));
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
